﻿using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace Inkubator.Wizard.WebWizard.WizardControl
{
    public class StepWizard : Panel
    {
        /// <summary>
        /// The name that the Wizard will show when this Step is loaded
        /// </summary>
        private string name;
        /// <summary>
        /// The description that the Wizard will place on the beginning of each Step
        /// </summary>
        private string description;
        /// <summary>
        /// A table that organize the different layout.
        /// </summary>
        private Table layout;
        /// <summary>
        /// A list that contain all the ids used in this step.
        /// </summary>
        private List<string> idControls;

        /// <summary>
        /// An ActionWizard that represent the action (DLL/EXE) to be perform.
        /// </summary>
        private ActionWizard action;
        /// <summary>
        /// The ID from the current Step.
        /// </summary>
        private string ID;

        public StepWizard()
        {
            layout = new Table();
            layout.CellSpacing = 5;

            this.idControls = new List<string>();

            this.Controls.Add(layout);
        }

        /// <summary>
        /// Add a component to the Layout
        /// </summary>
        /// <param name="label"></param>
        /// <param name="control"></param>
        public void addComponent(WebControl label, WebControl control)
        {
            TableRow row = new TableRow();

            TableCell cell = new TableCell();
            cell.Controls.Add(label);
            row.Cells.Add(cell);

            TableCell cell2 = new TableCell();
            cell2.Controls.Add(control);
            row.Cells.Add(cell2);

            layout.Rows.Add(row);
        }

        /// <summary>
        /// Add a component to the Layout
        /// </summary>
        /// <param name="control"></param>
        public void addComponent(WebControl control)
        {
            TableRow row = new TableRow();
            TableCell cell = new TableCell();
            cell.Controls.Add(control);
            row.Cells.Add(cell);

            layout.Rows.Add(row);
        }

        /// <summary>
        /// Set the name from the current step.
        /// </summary>
        /// <param name="name"></param>
        public void setName(string name)
        {
            this.name = name;
        }

        /// <summary>
        /// Return the name from the current step.
        /// </summary>
        /// <returns></returns>
        public string getName()
        {
            return this.name;
        }

        /// <summary>
        /// Set the description from the current step.
        /// </summary>
        /// <param name="description"></param>
        public void setDescription(string description)
        {
            this.description = description;
            Label lblDescription = new Label();
            lblDescription.Text = this.description;
            lblDescription.Height = 30;

            this.Controls.Clear();
            this.Controls.Add(lblDescription);
            this.Controls.Add(layout);
        }

        /// <summary>
        /// Return the description from the current step.
        /// </summary>
        /// <returns></returns>
        public string getDescription()
        {
            return this.description;
        }

        /// <summary>
        /// Add a new ID to the ID List to identify a new Control added to this step.
        /// </summary>
        /// <param name="id"></param>
        public void addIdControl(string id)
        {
            this.idControls.Add(id);
        }

        /// <summary>
        /// Return a WebControl that match with the ID in the argument.
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public WebControl obtainControl(string id)
        {
            WebControl control = (WebControl) layout.FindControl(id);
            return control;
        }

        /// <summary>
        /// Return a List with all the WebControls in the current step.
        /// </summary>
        /// <returns></returns>
        public List<WebControl> obtainAllControls()
        {
            List<WebControl> controls = new List<WebControl>();

            foreach (string id in this.idControls)
            {
                controls.Add(this.obtainControl(id));
            }

            return controls;
        }

        /// <summary>
        /// Set the ActionWizard object.
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <param name="method"></param>
        /// <param name="impInterface"></param>
        public void setAction(string assemblyName, string method, string impInterface)
        {
            this.action = new ActionWizard(assemblyName, method, impInterface);
        }

        /// <summary>
        /// Execute the action defined in the ActionWizard object.
        /// </summary>
        /// <param name="arguments"></param>
        public void executeAction(object[] arguments)
        {
            this.action.execute(arguments);
        }

        /// <summary>
        /// Return a boolean value that represent if the current step has an action assigned.
        /// </summary>
        /// <returns></returns>
        public bool hasAction()
        {
            if (this.action != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        public List<string> getIdControls()
        {
            return this.idControls;
        }

        public string getId()
        {
            return this.ID;
        }

        public void setId(string id)
        {
            this.ID = id;
        }

    }
}
